package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.health.pojo.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 这是用来处理检查项的controller
 * 1. 添加操作 ,
 * a. 传递上来的是json数据，要想把json数据接收转化到一个javaBean身上，需要加上注解
 */

@RequestMapping("/checkItem")
@RestController
public class CheckItemController {
    @Autowired
    private CheckItemService cs;

    /**
     * 添加检查项
     * @param checkItem
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {

       //定义result
        Result result = null;

        //调用service
        int row = cs.add(checkItem);
        //响应数据
        if (row > 0) {
            result = new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } else {
            result = new Result(true, MessageConstant.ADD_CHECKITEM_FAIL);

        }
        return result;

    }

    /**
     * 查询检查项
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        Result result = null;
        try {
            //1.调用service
            PageResult<CheckItem> pageResult = cs.findPage(queryPageBean);
            //2.响应

            result = new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
        } catch (Exception e) {
            result = new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_EDIT666')")
    public Result delete(int id) {
        int row = cs.delete(id);

        Result result = null;
        if (row > 0) {
            result = new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } else {
            result = new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return result;
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody CheckItem checkItem){

        int row = cs.update(checkItem);

        Result result =null;
        if(row>0){
            result = new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }else {
            result = new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return result;
    }

    /**
     * 查询所有的检查项 这里使用了RestFul的风格
     */
    @GetMapping("/checkItem")
    public Result findAll(){
        try {
            List<CheckItem> list =  cs.findAll();

            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);

        }
    }
}

